<?php

namespace app\modules\Product\models;

use Yii;
use app\modules\Cruise\models\Cruise;

/**
 * This is the model class for table "template".
 *
 * @property integer $id
 * @property integer $cruise_id
 * @property integer $from_city
 * @property integer $to_city
 * @property string $name
 * @property integer $line
 * @property string $cost
 * @property string $visa
 * @property string $trip
 * @property integer $status
 * @property string $created_at
 */
class Template extends \yii\db\ActiveRecord
{
     
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'template';
    }

    /**
     * @inheritdoc
     */
    public function rules() // 添加 途径城市 天数 todo
    {
        return [
            [['cruise_id','pass_city'], 'required'],
            [['cruise_id', 'from_city', 'to_city', 'line', 'status','days','nights', 'type','go_city','return_city'], 'integer'],
            [['cost', 'visa', 'trip','pass_city', 'nocost', 'cancel', 'discount', 'signup', 'land', 'expense','special','important','safe_1','safe_2','safe_3'], 'string'],
            [['created_at'], 'safe'],
            [['name'], 'string', 'max' => 128],
        ];
    }
    //  获取对应游轮   
    public function getCruise(){
        return $this->hasOne(Cruise::className(), ['id' => 'cruise_id']);
    }

    // 保存前处理
    public function beforeSave($insert) {//{{{
        if($insert){
        }
        $cruise = Cruise::findOne($this->cruise_id);
        if(!empty($this->pass_city)){
            $this->name = sprintf("\"%s\"%s%d晚%d天之旅",$cruise->name,$this->pass_city,$this->nights,$this->days);     
        }
       
        // 行程格式转换
        if(!empty($this->trip)){
            $trip = json_decode($this->trip,true);
            if(is_array($trip)){
                $arr = []; 
                foreach($trip['trip'] as $key=>$val){
                    
                    if(!empty(array_filter($val)) && !empty($val['7'])){
                        $arr[$key]['title']   = $val[0];
                        $arr[$key]['eat']     = $val[1];
                        $arr[$key]['room']    = $val[2];
                        $arr[$key]['country'] = $val[3];
                        $arr[$key]['time1']   = $val[4];
                        $arr[$key]['time2']   = $val[5];
                        $arr[$key]['desc']    = $val[6];
                        $type = Yii::$app->params['trip_type'];
                        $arr[$key]['type']   = !empty($val[7]) ? array_search($val[7], $type)  : NULL;
                    }
                }
                //$this->trip = json_encode($trip['trip']);
                $this->trip = json_encode($arr);
            }
        }
        // 获取途径城市 todo  
        
        return parent::beforeSave($insert);
    }//}}}


    /** 行程显示**/
    public function getTripstyle(){
        $trip = json_decode($this->trip, true);
        if(empty($trip)){
            return NULL; 
        }

        $html = "";
        
        foreach($trip  as $key=>$value){
           $html .= sprintf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
                            $value['title'], $value['eat'], $value['room'], $value['country'], $value['time1'], 
                            $value['time2'], Yii::$app->params['trip_type'][$value['type']]); 
        }
        
        $this->trip = sprintf("<table class='table'>%s</table>", $html);
        
        return $this->trip;
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'cruise_id' => Yii::t('app', 'Cruise ID'),
            'from_city' => Yii::t('app', 'From City'),
            'to_city' => Yii::t('app', 'To City'),
            'name' => Yii::t('app', 'Name'),
            'line' => Yii::t('app.cruise', 'Line'),
            'cost' => Yii::t('app.cruise', 'Cost'),
            'visa' => Yii::t('app.cruise', 'Visa'),
            'trip' => Yii::t('app', 'Trip'),
            'status' => Yii::t('app', 'Status'),
            'created_at' => Yii::t('app', 'Created At'),
            'pass_city' => Yii::t('app.cruise', 'Pass City'),
            'days' =>Yii::t('app.cruise', 'Days'),
            'nights' => Yii::t('app.cruise', 'Nights'),
            'nocost' => Yii::t('app.cruise', 'Nocost'),
            'cancel' => Yii::t('app.cruise', 'Cancel'),
            'discount' => Yii::t('app.cruise', 'Discount'),
            'signup' => Yii::t('app.cruise', 'Signup'),
            'land' => Yii::t('app.cruise', 'Land'),
            'expense' => Yii::t('app.cruise', 'Expense'),
            'type' => Yii::t('app.cruise', 'Type'),
            'go_city' => Yii::t('app.cruise', '上船城市'),
            'return_city' => Yii::t('app.cruise', '下船城市'),
            'special' => Yii::t('app.cruise', '产品特色'),
            'important' => Yii::t('app.cruise', '重要提示'),
            'safe_1' => Yii::t('app.cruise', '行前解约风险提醒'),
            'safe_2' => Yii::t('app.cruise', '人身财产安全警示'),
            'safe_3' => Yii::t('app.cruise', '第三方责任告知'),
        ];
    }
}
